<?php

class Shell_Command_UserDel extends Shell_Command_Abstract_CommandBase implements Shell_Command_Abstract_IShellCommand{
//*************************************************************************************************
		public function __construct() 
		{
			parent::__construct();
			$this->NAME = "userdel";
		}
		
//*************************************************************************************************
		public function execute($arguments) 
		{						
			if (count($arguments)==0)
			{
				echo "\nNo username provided! Usage: userdel <username>\n\n";
				return;
			}
			$userName = $arguments[0];
			
			// initialize UMS
			$sqlitePath = Core_Registry::read(Enum_RegistryItems::SQLITE_PATH);				
			$adapter = new System_UMS_SQLiteAdapter($sqlitePath);
			$ums = new System_UMS($adapter);
			
			/////////////////////////////////////////////
			// check if user exists
			$user = $ums->userGet($userName);
			if ($user == null)
			{
				$this->parseErrorCode(-1, "", "User $userName does not exist!");
				return;
			}
			/////////////////////////////////////////////
			
			$confirmation = Core_IO::readUserInput("Delete user ".$userName." from the system? Y/N: ", true);
			
			// accepts any word starting with 'y' or 'Y' as a confirmation
			if (strtolower($confirmation[0]) != 'y')
			{				
				echo "Cancelled";
				return;
			}
			
			$sqlitePath = Core_Registry::read(Enum_RegistryItems::SQLITE_PATH);				
			$adapter = new System_UMS_SQLiteAdapter($sqlitePath);
			$ums = new System_UMS($adapter);
			
			$errorCode = $ums->userDel($userName);
			
			$positiveMsg = "User $userName was sucessfully deleted."; 
			$negativeMsg = "Failed to delete $userName!";			
			$this->parseErrorCode($errorCode, $positiveMsg, $negativeMsg);
			
		}
		
//*************************************************************************************************		
		public function __toString() {
			return "[UserDel]";
		}
}


// *****************************************************
// Part #3 - Modifying a line of screen text
// *****************************************************
/*
$counter2 = $counter;
while($counter2 >= 0){
  $output_str =  "Current count is ".$counter2." out of ".$counter.". ";
  echo $output_str;
  $line_size = strlen($output_str);
  while($line_size >= 0){
    echo "\010";
    $line_size--;
  }
  $counter2--;
  sleep(1);
}
*/
// *****************************************************